﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;

namespace HangerSupport
{
	public class PipeSelect
	{
		public PipeSelect(UIApplication uiApp)
		{
			this.m_uiApp = uiApp;
			this.m_doc = uiApp.ActiveUIDocument.Document;
			this.m_selection = uiApp.ActiveUIDocument.Selection;
			this.m_listElementSel = new List<Element>();
			this.m_listSecSel = new List<PipeSectionDef>();
			this.m_ptPosition = null;
			this.m_dAngle = 0.0;
			this.m_bMirrored = false;
		}

		public bool IsNoSelect()
		{
			return this.m_listSecSel.Count == 0;
		}

		public List<PipeSectionDef> GetSelSecLst()
		{
			return this.m_listSecSel;
		}

		public void Clear()
		{
			this.m_listSecSel.Clear();
		}

		public void ClearAll()
		{
			this.m_listSecSel.Clear();
			this.m_listElementSel.Clear();
		}

		public XYZ GetDrawMidPoint(XYZ ptBase)
		{
			bool flag = this.m_ptPosition == null || this.m_vtPipe == null;
			XYZ result;
			if (flag)
			{
				result = ptBase;
			}
			else
			{
				Line line = Line.CreateUnbound(this.m_ptPosition, this.m_vtPipe);
				XYZ xyzpoint = line.Project(ptBase).XYZPoint;
				result = xyzpoint;
			}
			return result;
		}

		public List<PipeSectionDef> SortSectionsByElevation(List<PipeSectionDef> lstTemp)
		{
			List<PipeSectionDef> list = new List<PipeSectionDef>();
			while (lstTemp.Count > 0)
			{
				PipeSectionDef item = lstTemp[0];
				int index = 0;
				for (int i = 1; i < lstTemp.Count; i++)
				{
					PipeSectionDef pipeSectionDef = lstTemp[i];
					bool flag = item.ElevationBottom() > pipeSectionDef.ElevationBottom();
					if (flag)
					{
						item = pipeSectionDef;
						index = i;
					}
				}
				list.Add(item);
				lstTemp.RemoveAt(index);
			}
			return list;
		}

		public List<double> GetLevelsElevation(double dHorGap, double dHorBarHigh)
		{
			bool flag = this.m_listSecSel.Count == 0;
			List<double> result;
			if (flag)
			{
				result = null;
			}
			else
			{
				List<double> list = new List<double>();
				double num = 0.0;
				double num2 = 0.0;
				List<PipeSectionDef> list2 = new List<PipeSectionDef>();
				foreach (PipeSectionDef item in this.m_listSecSel)
				{
					list2.Add(item);
				}
				list2 = this.SortSectionsByElevation(list2);
				for (;;)
				{
					bool flag2 = list2.Count == 0;
					if (flag2)
					{
						break;
					}
					int index = 0;
					for (int i = 0; i < list2.Count; i++)
					{
						PipeSectionDef pipeSectionDef = list2[i];
						double num3 = pipeSectionDef.ElevationBottom() - dHorGap;
						double num4 = pipeSectionDef.ElevationTop() + dHorGap;
						bool flag3 = i == 0;
						if (flag3)
						{
							num = num3;
							num2 = num4;
							index = i;
						}
						else
						{
							bool flag4 = num > num3;
							if (flag4)
							{
								num = num3;
								num2 = num4;
								index = i;
							}
						}
					}
					list2.RemoveAt(index);
					for (int j = 0; j < list2.Count; j++)
					{
						PipeSectionDef pipeSectionDef2 = list2[j];
						double num5 = pipeSectionDef2.ElevationBottom() - dHorGap;
						double num6 = pipeSectionDef2.ElevationTop() + dHorGap;
						bool flag5 = num5 - dHorBarHigh > num2 || num6 + dHorBarHigh < num;
						if (!flag5)
						{
							bool flag6 = num5 > num || num5 < num2;
							if (flag6)
							{
								list2.RemoveAt(j);
								j--;
								bool flag7 = num6 > num2;
								if (flag7)
								{
									num2 = num6;
								}
							}
						}
					}
					list.Add(Utils.DoublePrecision(num, 0));
				}
				num2 = Utils.DoublePrecision(num2 + dHorBarHigh, 0);
				list.Add(num2);
				result = list;
			}
			return result;
		}

		public List<double> GetLevelElevationWides(ref double dWideTolCurLevel, List<double> listElevation, double dVerGap, double dVerBarHigh, int nEleIndex = 0)
		{
			bool flag = listElevation.Count == 0;
			List<double> result;
			if (flag)
			{
				result = null;
			}
			else
			{
				dWideTolCurLevel = 0.0;
				double num = listElevation[nEleIndex];
				double num2 = num + 100000.0;
				bool flag2 = nEleIndex < listElevation.Count - 1;
				if (flag2)
				{
					num2 = listElevation[nEleIndex + 1];
				}
				List<PipeSectionDef> list = new List<PipeSectionDef>();
				foreach (PipeSectionDef item in this.m_listSecSel)
				{
					double num3 = item.ElevationBottom();
					double num4 = item.ElevationTop();
					double num5 = (num4 + num3) / 2.0;
					bool flag3 = num5 > num && num5 < num2;
					if (flag3)
					{
						list.Add(item);
					}
				}
				bool flag4 = list.Count == 0;
				if (flag4)
				{
					result = null;
				}
				else
				{
					List<double> list2 = new List<double>();
					for (int i = 0; i < list.Count - 1; i++)
					{
						for (int j = i + 1; j < list.Count; j++)
						{
							double dDistX = list[i].m_dDistX;
							double dDistX2 = list[j].m_dDistX;
							bool flag5 = dDistX > dDistX2;
							if (flag5)
							{
								PipeSectionDef value = list[i];
								list[i] = list[j];
								list[j] = value;
							}
						}
					}
					double num6 = list[0].m_dDistX;
					double num7 = list[0].m_dDistX;
					foreach (PipeSectionDef pipeSectionDef in list)
					{
						double num8 = pipeSectionDef.Wide();
						double num9 = pipeSectionDef.m_dDistX - num8 / 2.0;
						double num10 = pipeSectionDef.m_dDistX + num8 / 2.0;
						bool flag6 = num6 > num9;
						if (flag6)
						{
							num6 = num9;
						}
						bool flag7 = num7 < num10;
						if (flag7)
						{
							num7 = num10;
						}
					}
					double num11 = dVerGap * 2.0 + dVerBarHigh;
					for (int k = 0; k < list.Count - 1; k++)
					{
						PipeSectionDef pipeSectionDef2 = list[k];
						PipeSectionDef pipeSectionDef3 = list[k + 1];
						double num12 = pipeSectionDef2.m_dDistX + pipeSectionDef2.Wide() / 2.0;
						double num13 = pipeSectionDef3.m_dDistX - pipeSectionDef3.Wide() / 2.0;
						bool flag8 = num12 + num11 < num13;
						if (flag8)
						{
							list2.Add((num12 + num13) / 2.0);
						}
					}
					double num14 = this.m_listSecSel[0].m_dDistX;
					double num15 = this.m_listSecSel[0].m_dDistX;
					foreach (PipeSectionDef pipeSectionDef4 in this.m_listSecSel)
					{
						double num16 = pipeSectionDef4.Wide();
						double num17 = pipeSectionDef4.m_dDistX - num16 / 2.0;
						double num18 = pipeSectionDef4.m_dDistX + num16 / 2.0;
						bool flag9 = num14 > num17;
						if (flag9)
						{
							num14 = num17;
						}
						bool flag10 = num15 < num18;
						if (flag10)
						{
							num15 = num18;
						}
					}
					dWideTolCurLevel = num15 - num6;
					List<double> list3 = new List<double>();
					for (int l = list2.Count - 1; l >= 0; l--)
					{
						list3.Add(Utils.DoublePrecision(num15 - list2[l], 1));
					}
					dWideTolCurLevel = Utils.DoublePrecision(dWideTolCurLevel, 1);
					result = list3;
				}
			}
			return result;
		}

		public double GetWideInLevelElevationFromDist(double dEleTop, double dEleBottom, double dVerGap, double dVerBarHigh, double dStartDist)
		{
			List<PipeSectionDef> list = new List<PipeSectionDef>();
			foreach (PipeSectionDef pipeSectionDef in this.m_listSecSel)
			{
				double dElevation = pipeSectionDef.m_dElevation;
				bool flag = dElevation > dEleBottom && dElevation < dEleTop;
				if (flag)
				{
					list.Add(pipeSectionDef);
				}
			}
			bool flag2 = list.Count == 0;
			double result;
			if (flag2)
			{
				result = dStartDist;
			}
			else
			{
				for (int i = 0; i < list.Count - 1; i++)
				{
					for (int j = i + 1; j < list.Count; j++)
					{
						double dDistX = list[i].m_dDistX;
						double dDistX2 = list[j].m_dDistX;
						bool flag3 = dDistX < dDistX2;
						if (flag3)
						{
							PipeSectionDef value = list[i];
							list[i] = list[j];
							list[j] = value;
						}
					}
				}
				double num = 0.0;
				double num2 = 0.0;
				this.GetSecX(ref num, ref num2);
				double num3 = num2 - dStartDist - dVerGap - dVerBarHigh / 2.0;
				for (int k = 0; k < list.Count; k++)
				{
					double num4 = list[k].Wide();
					double dDistX3 = list[k].m_dDistX;
					double num5 = dDistX3 - num4 / 2.0 - dVerGap;
					double num6 = dDistX3 + num4 / 2.0 + dVerGap;
					bool flag4 = k == 0;
					if (flag4)
					{
						bool flag5 = num3 > num6;
						if (flag5)
						{
							return dStartDist;
						}
					}
					else
					{
						double num7 = list[k - 1].Wide();
						double dDistX4 = list[k - 1].m_dDistX;
						double num8 = dDistX4 - num7 / 2.0 - dVerGap;
						bool flag6 = num3 < num8;
						if (!flag6)
						{
							bool flag7 = Math.Abs(num6 - num8) < dVerBarHigh;
							if (!flag7)
							{
								return Math.Abs(num2 - num8 + 2.0 * dVerGap);
							}
						}
					}
				}
				result = this.GetSecWideTol(dVerGap);
			}
			return result;
		}

		public double GetPXDJOffsetWide(List<double> listElevation, double dVerGap)
		{
			double secWideTol = this.GetSecWideTol(dVerGap);
			bool flag = listElevation.Count == 0;
			double result;
			if (flag)
			{
				result = secWideTol;
			}
			else
			{
				double num = listElevation[0];
				double num2 = num + 100000.0;
				bool flag2 = listElevation.Count > 1;
				if (flag2)
				{
					num2 = listElevation[1];
				}
				List<PipeSectionDef> list = new List<PipeSectionDef>();
				foreach (PipeSectionDef item in this.m_listSecSel)
				{
					double num3 = item.ElevationBottom();
					double num4 = item.ElevationTop();
					double num5 = (num4 + num3) / 2.0;
					bool flag3 = num5 > num && num5 < num2;
					if (flag3)
					{
						list.Add(item);
					}
				}
				double num6 = list[0].m_dDistX;
				foreach (PipeSectionDef pipeSectionDef in this.m_listSecSel)
				{
					double num7 = pipeSectionDef.Wide();
					double num8 = pipeSectionDef.m_dDistX - num7 / 2.0;
					double num9 = pipeSectionDef.m_dDistX + num7 / 2.0;
					bool flag4 = num6 < num9;
					if (flag4)
					{
						num6 = num9;
					}
				}
				double num10 = list[0].m_dDistX;
				double num11 = list[0].m_dDistX;
				num11 = num6;
				foreach (PipeSectionDef pipeSectionDef2 in list)
				{
					double num12 = pipeSectionDef2.Wide();
					double num13 = pipeSectionDef2.m_dDistX - num12 / 2.0;
					double num14 = pipeSectionDef2.m_dDistX + num12 / 2.0;
					bool flag5 = num10 > num13;
					if (flag5)
					{
						num10 = num13;
					}
					bool flag6 = num11 < num14;
					if (flag6)
					{
						num11 = num14;
					}
				}
				double num15 = num11 - num10 + 2.0 * dVerGap;
				num15 = (double)((int)(num15 + 0.5));
				result = num15;
			}
			return result;
		}

		public double GetSecWideTol(double dVerOffset)
		{
			bool flag = this.m_listSecSel.Count == 0;
			double result;
			if (flag)
			{
				result = 0.0;
			}
			else
			{
				double num = this.m_listSecSel[0].m_dDistX;
				double num2 = this.m_listSecSel[0].m_dDistX;
				foreach (PipeSectionDef pipeSectionDef in this.m_listSecSel)
				{
					double num3 = pipeSectionDef.Wide();
					double num4 = pipeSectionDef.m_dDistX - num3 / 2.0;
					double num5 = pipeSectionDef.m_dDistX + num3 / 2.0;
					bool flag2 = num > num4;
					if (flag2)
					{
						num = num4;
					}
					bool flag3 = num2 < num5;
					if (flag3)
					{
						num2 = num5;
					}
				}
				double num6 = num2 - num + 2.0 * dVerOffset;
				num6 = (double)((int)(num6 + 0.5));
				result = num6;
			}
			return result;
		}

		public double GetSecWideTolByElevationUp(double dVerOffset, double dElevation)
		{
			bool flag = this.m_listSecSel.Count == 0;
			double result;
			if (flag)
			{
				result = 0.0;
			}
			else
			{
				double num = this.m_listSecSel[0].m_dDistX;
				double num2 = this.m_listSecSel[0].m_dDistX;
				bool flag2 = false;
				foreach (PipeSectionDef pipeSectionDef in this.m_listSecSel)
				{
					double num3 = pipeSectionDef.ElevationBottom();
					bool flag3 = num3 < dElevation;
					if (!flag3)
					{
						double num4 = pipeSectionDef.Wide();
						double num5 = pipeSectionDef.m_dDistX - num4 / 2.0;
						double num6 = pipeSectionDef.m_dDistX + num4 / 2.0;
						bool flag4 = !flag2;
						if (flag4)
						{
							num = num5;
							num2 = num6;
							flag2 = true;
						}
						bool flag5 = num > num5;
						if (flag5)
						{
							num = num5;
						}
						bool flag6 = num2 < num6;
						if (flag6)
						{
							num2 = num6;
						}
					}
				}
				double num7 = 0.0;
				double num8 = 0.0;
				this.GetSecX(ref num7, ref num8);
				double num9 = num8 - num + 2.0 * dVerOffset;
				num9 = Utils.DoublePrecision(num9, 1);
				result = num9;
			}
			return result;
		}

		public double GetSecMidX()
		{
			bool flag = this.m_listSecSel.Count == 0;
			double result;
			if (flag)
			{
				result = 0.0;
			}
			else
			{
				double num = 0.0;
				double num2 = 0.0;
				this.GetSecX(ref num, ref num2);
				double num3 = (num2 + num) / 2.0;
				result = num3;
			}
			return result;
		}

		public void GetSecX(ref double dWideMin, ref double dWideMax)
		{
			bool flag = this.m_listSecSel.Count == 0;
			if (!flag)
			{
				dWideMin = this.m_listSecSel[0].m_dDistX;
				dWideMax = this.m_listSecSel[0].m_dDistX;
				bool flag2 = this.m_listSecSel.Count == 0;
				if (!flag2)
				{
					foreach (PipeSectionDef pipeSectionDef in this.m_listSecSel)
					{
						double num = pipeSectionDef.Wide();
						double num2 = pipeSectionDef.m_dDistX - num / 2.0;
						double num3 = pipeSectionDef.m_dDistX + num / 2.0;
						bool flag3 = dWideMin > num2;
						if (flag3)
						{
							dWideMin = num2;
						}
						bool flag4 = dWideMax < num3;
						if (flag4)
						{
							dWideMax = num3;
						}
					}
				}
			}
		}

		public bool SelectPipe(bool bIncludeVerPipe = true)
		{
			this.m_listElementSel.Clear();
			IList<Reference> list = new List<Reference>();
			try
			{
				if (bIncludeVerPipe)
				{
					list = this.m_selection.PickObjects((Autodesk.Revit.UI.Selection.ObjectType)1, new PipeSelectionFilter(false), "请选择风管 管道 桥架 线管等，布置支吊架");
				}
				else
				{
					list = this.m_selection.PickObjects((Autodesk.Revit.UI.Selection.ObjectType)1, new PipeSelectionFilterNoVerPipe(), "请选择风管 管道 桥架 线管等，布置支吊架");
				}
			}
			catch (Exception ex)
			{
				return false;
			}
			foreach (Reference reference in list)
			{
				bool flag = reference != null && reference.ElementId != ElementId.InvalidElementId;
				if (flag)
				{
					Element element = this.m_doc.GetElement(reference.ElementId);
					bool flag2 = !bIncludeVerPipe;
					if (flag2)
					{
						bool flag3 = Utils.IsVerticalPipe(element);
						if (flag3)
						{
							continue;
						}
					}
					this.m_listElementSel.Add(element);
				}
			}
			return this.GetSecInfo();
		}

		public Element SelectSinglePipe(string str = "请选择风管、管道、桥架", bool bOnlyHorPipe = true)
		{
			XYZ xyz = null;
			return this.SelectSinglePipe(ref xyz, str, bOnlyHorPipe);
		}

		public Element SelectSinglePipe(ref XYZ pt, string str = "请选择风管、管道、桥架", bool bOnlyHorPipe = true)
		{
			Reference reference;
			try
			{
				reference = this.m_selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, new PipeSelectionFilter(bOnlyHorPipe), str);
			}
			catch (Exception ex)
			{
				return null;
			}
			bool flag = reference != null && reference.ElementId != ElementId.InvalidElementId;
			Element result;
			if (flag)
			{
				Element element = this.m_doc.GetElement(reference.ElementId);
				pt = reference.GlobalPoint;
				this.m_ptPick = pt;
				pt = Utils.PtTo2d(pt);
				this.m_listElementSel.Add(element);
				this.GetSecInfo();
				result = element;
			}
			else
			{
				result = null;
			}
			return result;
		}

		public Element SelectSinglePipeAll(bool bIncludeVerPipe = true, string str = "请选择风管、管道、桥架")
		{
			Reference reference;
			try
			{
				if (bIncludeVerPipe)
				{
					reference = this.m_selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, new PipeSelectionFilter(false), str);
				}
				else
				{
					reference = this.m_selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, new PipeSelectionFilterNoVerPipe(), str);
				}
			}
			catch (Exception ex)
			{
				return null;
			}
			bool flag = reference != null && reference.ElementId != ElementId.InvalidElementId;
			Element result;
			if (flag)
			{
				this.m_ptPick = reference.GlobalPoint;
				Element element = this.m_doc.GetElement(reference.ElementId);
				this.m_listElementSel.Clear();
				bool flag2 = !bIncludeVerPipe;
				if (flag2)
				{
					bool flag3 = Utils.IsVerticalPipe(element);
					if (flag3)
					{
						return null;
					}
				}
				this.m_listElementSel.Add(element);
				this.GetSecInfo();
				result = element;
			}
			else
			{
				result = null;
			}
			return result;
		}

		public Element SelectSingleWall(ref XYZ pt, string str = "请选择墙")
		{
			Reference reference;
			try
			{
				reference = this.m_selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, new WallSelectionFilter(), str);
			}
			catch (Exception ex)
			{
				return null;
			}
			bool flag = reference != null && reference.ElementId != ElementId.InvalidElementId;
			Element result;
			if (flag)
			{
				Element element = this.m_doc.GetElement(reference.ElementId);
				pt = reference.GlobalPoint;
				pt = Utils.PtTo2d(pt);
				result = element;
			}
			else
			{
				result = null;
			}
			return result;
		}

		public Element SelectSingleHS(string str = "请选择支吊架")
		{
			Element result = null;
			try
			{
				Reference reference = this.m_selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, new HSFilter(), str);
				result = this.m_doc.GetElement(reference);
			}
			catch (Exception)
			{
				return null;
			}
			return result;
		}

		public Element SelectSingleHSFamily(string strFamilyName, string str = "请选择支吊架")
		{
			Element result = null;
			try
			{
				Reference reference = this.m_selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, new HSTypeFilter(strFamilyName), str);
				result = this.m_doc.GetElement(reference);
			}
			catch (Exception)
			{
				return null;
			}
			return result;
		}

		public List<Element> SelectMultiHS(string str = "请选择支吊架")
		{
			List<Element> list = new List<Element>();
			try
			{
				IList<Reference> list2 = this.m_selection.PickObjects((Autodesk.Revit.UI.Selection.ObjectType)1, new HSFilter(), str);
				foreach (Reference reference in list2)
				{
					Element element = this.m_doc.GetElement(reference);
					list.Add(element);
				}
			}
			catch (Exception)
			{
				return list;
			}
			return list;
		}

		public List<Element> SelectMultiHSFamily(string strFamilyName, string str = "请选择支吊架")
		{
			List<Element> list = new List<Element>();
			try
			{
				IList<Reference> list2 = this.m_selection.PickObjects((Autodesk.Revit.UI.Selection.ObjectType)1, new HSTypeFilter(strFamilyName), str);
				foreach (Reference reference in list2)
				{
					Element element = this.m_doc.GetElement(reference);
					list.Add(element);
				}
			}
			catch (Exception)
			{
				return list;
			}
			return list;
		}

		public List<Element> SelectMultiHanger(string str = "请选择吊架")
		{
			List<Element> list = new List<Element>();
			try
			{
				IList<Reference> list2 = this.m_selection.PickObjects((Autodesk.Revit.UI.Selection.ObjectType)1, new HangerFilter(), str);
				foreach (Reference reference in list2)
				{
					Element element = this.m_doc.GetElement(reference);
					list.Add(element);
				}
			}
			catch (Exception)
			{
				return list;
			}
			return list;
		}

		public static PipeSectionDef GetPipeSectionSize(Element ele, Autodesk.Revit.DB.Document doc)
		{
			PipeSectionDef pipeSectionDef = default(PipeSectionDef);
			MEPCurve mepcurve = ele as MEPCurve;
			bool flag = mepcurve == null;
			PipeSectionDef result;
			if (flag)
			{
				result = pipeSectionDef;
			}
			else
			{
				string strLevelName = "";
				double dLevelOffset = 0.0;
				Parameter parameter = ele.GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_START_LEVEL_PARAM);
				bool flag2 = parameter != null;
				if (flag2)
				{
					Level level = (Level)doc.GetElement(parameter.AsElementId());
					bool flag3 = level != null;
					if (flag3)
					{
						strLevelName = level.Name;
					}
				}
				Parameter parameter2 = ele.GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_OFFSET_PARAM);
				bool flag4 = parameter2 != null;
				if (flag4)
				{
					dLevelOffset = HSFamily.feetToMM(parameter2.AsDouble());
				}
				pipeSectionDef.m_ElementId = ele.Id;
				pipeSectionDef.m_strFamilyName = ele.Category.Name;
				pipeSectionDef.m_strLevelName = strLevelName;
				pipeSectionDef.m_dLevelOffset = dLevelOffset;
				ConnectorSet connectors = mepcurve.ConnectorManager.Connectors;
				foreach (object obj in connectors)
				{
					Connector connector = (Connector)obj;
					bool flag5 = (int)connector.ConnectorType != 1;
					if (!flag5)
					{
						pipeSectionDef.m_dElevation = Utils.DoublePrecision(HSFamily.feetToMM(connector.Origin.Z), 1);
						bool flag6 = (int)connector.Shape == 0;
						if (flag6)
						{
							pipeSectionDef.m_eSecType = SectionType.kCircle;
							pipeSectionDef.m_dWide = Utils.DoublePrecision(HSFamily.feetToMM(connector.Radius) * 2.0, 1);
							Pipe pipe = ele as Pipe;
							bool flag7 = pipe != null;
							if (flag7)
							{
								Parameter parameter3 = pipe.GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_PIPE_OUTER_DIAMETER);
								bool flag8 = parameter3 != null;
								if (flag8)
								{
									double feetVal = parameter3.AsDouble();
									pipeSectionDef.m_dWide = Utils.DoublePrecision(HSFamily.feetToMM(feetVal), 1);
								}
							}
						}
						else
						{
							bool flag9 = (int)connector.Shape == 1;
							if (flag9)
							{
								pipeSectionDef.m_eSecType = SectionType.kRect;
								pipeSectionDef.m_dWide = Utils.DoublePrecision(HSFamily.feetToMM(connector.Width), 1);
								pipeSectionDef.m_dHigh = Utils.DoublePrecision(HSFamily.feetToMM(connector.Height), 1);
							}
							else
							{
								bool flag10 = (int)connector.Shape == 2;
								if (!flag10)
								{
									return pipeSectionDef;
								}
								pipeSectionDef.m_eSecType = SectionType.kOval;
								pipeSectionDef.m_dWide = Utils.DoublePrecision(HSFamily.feetToMM(connector.Width), 1);
								pipeSectionDef.m_dHigh = Utils.DoublePrecision(HSFamily.feetToMM(connector.Height), 1);
							}
						}
						break;
					}
				}
				result = pipeSectionDef;
			}
			return result;
		}

		public bool SelectPipeByLine(XYZ ptInS, XYZ ptInE)
		{
			this.m_listElementSel.Clear();
			IList<Element> list = new List<Element>();
			IList<Element> list2 = HSDraw.GetElementsCurLevel(this.m_doc).ToElements();
			bool flag = list2 == null;
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				foreach (Element element in list2)
				{
					bool flag2 = PipeSelectionFilter.IsPipeDuct(element) && !Utils.IsVerticalPipe(element);
					if (flag2)
					{
						list.Add(element);
					}
				}
				foreach (Element item in list)
				{
					this.m_listElementSel.Add(item);
				}
				result = this.GetSecInfoByLine(ptInS, ptInE, false);
			}
			return result;
		}

		public bool GetSecInfoByLine(XYZ ptInS, XYZ ptInE, bool bExtend = false)
		{
			bool flag = this.m_listElementSel.Count<Element>() == 0;
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				this.m_listSecSel.Clear();
				Line ln = Line.CreateBound(ptInS, ptInE);
				List<XYZ> list = new List<XYZ>();
				XYZ xyz = null;
				XYZ xyz2 = XYZ.BasisX;
				XYZ xyz3 = (ptInE - ptInS).Normalize();
				xyz2 = xyz3;
				xyz2 = xyz2.CrossProduct(XYZ.BasisZ);
				this.m_vtPipe = xyz2;
				xyz2 = xyz3;
				this.m_dAngle = Utils.Angle(xyz2);
				bool bMirrored = this.m_bMirrored;
				if (bMirrored)
				{
					this.m_dAngle += Math.PI;
				}
				XYZ xyz4 = null;
				for (int i = 0; i < this.m_listElementSel.Count<Element>(); i++)
				{
					Element element = this.m_listElementSel[i];
					MEPCurve mepcurve = element as MEPCurve;
					bool flag2 = mepcurve == null;
					if (!flag2)
					{
						LocationCurve locationCurve = mepcurve.Location as LocationCurve;
						Line line = locationCurve.Curve as Line;
						bool flag3 = line == null;
						if (!flag3)
						{
							XYZ endPoint = line.GetEndPoint(0);
							XYZ endPoint2 = line.GetEndPoint(1);
							XYZ xyz5 = Utils.PtTo2d(endPoint);
							XYZ xyz6 = Utils.PtTo2d(endPoint2);
							bool flag4 = xyz5.IsAlmostEqualTo(xyz6, 1E-05);
							if (!flag4)
							{
								XYZ xyz7 = xyz5.Subtract(xyz6).Normalize();
								XYZ xyz8 = new XYZ(0.0, 0.0, 1.0);
								bool flag5 = xyz7.IsAlmostEqualTo(xyz8, 0.0032808398950131233) || xyz7.IsAlmostEqualTo(xyz8 * -1.0, 0.0032808398950131233);
								if (!flag5)
								{
									bool flag6 = !bExtend;
									Line ln2;
									if (flag6)
									{
										ln2 = Line.CreateBound(xyz5, xyz6);
									}
									else
									{
										ln2 = Line.CreateUnbound(xyz5, xyz6 - xyz5);
										line = Line.CreateUnbound(endPoint, endPoint2 - endPoint);
									}
									XYZ xyz9 = null;
									bool flag7 = !Utils.getLineCrossPoint(ref xyz9, ln2, ln);
									if (!flag7)
									{
										XYZ xyz10 = null;
										Line ln3 = Line.CreateUnbound(xyz9, XYZ.BasisZ);
										bool flag8 = !Utils.getLineCrossPoint(ref xyz10, line, ln3);
										if (!flag8)
										{
											bool flag9 = xyz4 == null;
											if (flag9)
											{
												xyz4 = xyz10;
											}
											double dVal = 0.0;
											bool flag10 = xyz == null;
											if (flag10)
											{
												xyz = new XYZ(xyz9.X, xyz9.Y, 0.0);
											}
											else
											{
												XYZ xyz11 = new XYZ(xyz9.X, xyz9.Y, 0.0);
												XYZ xyz12 = (xyz11 - xyz).Normalize();
												dVal = HSFamily.feetToMM((xyz11 - xyz).GetLength() * (double)(xyz12.IsAlmostEqualTo(xyz2) ? 1 : -1));
											}
											string strLevelName = "";
											Parameter parameter = element.GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_START_LEVEL_PARAM);
											bool flag11 = parameter != null;
											if (flag11)
											{
												Level level = (Level)this.m_doc.GetElement(parameter.AsElementId());
												bool flag12 = level != null;
												if (flag12)
												{
													strLevelName = level.Name;
												}
											}
											Parameter parameter2 = element.GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_OFFSET_PARAM);
											bool flag13 = parameter2 != null;
											double num;
											if (flag13)
											{
												num = HSFamily.feetToMM(parameter2.AsDouble());
											}
											num = HSFamily.feetToMM(xyz10.Z);
											PipeSectionDef item = default(PipeSectionDef);
											item.m_dDistX = Utils.DoublePrecision(dVal, 1);
											item.m_ElementId = element.Id;
											item.m_strFamilyName = element.Category.Name;
											item.m_strLevelName = strLevelName;
											item.m_dLevelOffset = num;
											item.m_dElevation = num;
											ConnectorSet connectors = mepcurve.ConnectorManager.Connectors;
											foreach (object obj in connectors)
											{
												Connector connector = (Connector)obj;
												bool flag14 = (int)connector.ConnectorType != 1;
												if (!flag14)
												{
													double num2 = Utils.DoublePrecision(HSFamily.feetToMM(connector.Origin.Z), 1);
													bool flag15 = (int)connector.Shape == 0;
													if (flag15)
													{
														item.m_eSecType = SectionType.kCircle;
														item.m_dWide = Utils.DoublePrecision(HSFamily.feetToMM(connector.Radius) * 2.0, 1);
														Pipe pipe = element as Pipe;
														bool flag16 = pipe != null;
														if (flag16)
														{
															Parameter parameter3 = pipe.GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_PIPE_OUTER_DIAMETER);
															bool flag17 = parameter3 != null;
															if (flag17)
															{
																double feetVal = parameter3.AsDouble();
																item.m_dWide = Utils.DoublePrecision(HSFamily.feetToMM(feetVal), 1);
															}
														}
													}
													else
													{
														bool flag18 = (int)connector.Shape == 1;
														if (flag18)
														{
															item.m_eSecType = SectionType.kRect;
															item.m_dWide = Utils.DoublePrecision(HSFamily.feetToMM(connector.Width), 1);
															item.m_dHigh = Utils.DoublePrecision(HSFamily.feetToMM(connector.Height), 1);
														}
														else
														{
															bool flag19 = (int)connector.Shape == 2;
															if (!flag19)
															{
																return false;
															}
															item.m_eSecType = SectionType.kOval;
															item.m_dWide = Utils.DoublePrecision(HSFamily.feetToMM(connector.Width), 1);
															item.m_dHigh = Utils.DoublePrecision(HSFamily.feetToMM(connector.Height), 1);
														}
													}
													break;
												}
											}
											this.m_listSecSel.Add(item);
										}
									}
								}
							}
						}
					}
				}
				bool flag20 = this.m_listSecSel.Count == 0;
				if (flag20)
				{
					result = false;
				}
				else
				{
					this.m_vtSection = xyz2;
					this.m_ptPosition = xyz + HSFamily.mmToFeet(this.GetSecMidX()) * xyz2;
					result = true;
				}
			}
			return result;
		}

		public bool GetSecInfoByLine(Line line, bool bExtend = false)
		{
			bool flag = line == null;
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				XYZ endPoint = line.GetEndPoint(0);
				XYZ endPoint2 = line.GetEndPoint(1);
				result = this.GetSecInfoByLine(endPoint, endPoint2, bExtend);
			}
			return result;
		}

		public bool GetSecInfo()
		{
			return this.GetSecInfoFromSelPipes();
		}

		public bool GetSecInfoFromSelVerPipe(Element ele)
		{
			this.m_listSecSel.Clear();
			PipeSectionDef item = default(PipeSectionDef);
			item.m_dDistX = 0.0;
			item.m_ElementId = ele.Id;
			item.m_strFamilyName = ele.Category.Name;
			item.m_strLevelName = "";
			item.m_dLevelOffset = 0.0;
			MEPCurve mepcurve = ele as MEPCurve;
			ConnectorSet connectors = mepcurve.ConnectorManager.Connectors;
			foreach (object obj in connectors)
			{
				Connector connector = (Connector)obj;
				bool flag = (int)connector.ConnectorType != 1;
				if (!flag)
				{
					item.m_dElevation = Utils.DoublePrecision(HSFamily.feetToMM(connector.Origin.Z), 1);
					bool flag2 = (int)connector.Shape == 0;
					if (flag2)
					{
						item.m_eSecType = SectionType.kCircle;
						item.m_dWide = Utils.DoublePrecision(HSFamily.feetToMM(connector.Radius) * 2.0, 1);
						Pipe pipe = ele as Pipe;
						bool flag3 = pipe != null;
						if (flag3)
						{
							Parameter parameter = pipe.GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_PIPE_OUTER_DIAMETER);
							bool flag4 = parameter != null;
							if (flag4)
							{
								double feetVal = parameter.AsDouble();
								item.m_dWide = Utils.DoublePrecision(HSFamily.feetToMM(feetVal), 1);
							}
						}
					}
					else
					{
						bool flag5 = (int)connector.Shape == 1;
						if (flag5)
						{
							item.m_eSecType = SectionType.kRect;
							item.m_dWide = Utils.DoublePrecision(HSFamily.feetToMM(connector.Width), 1);
							item.m_dHigh = Utils.DoublePrecision(HSFamily.feetToMM(connector.Height), 1);
						}
						else
						{
							bool flag6 = (int)connector.Shape == 2;
							if (!flag6)
							{
								return false;
							}
							item.m_eSecType = SectionType.kOval;
							item.m_dWide = Utils.DoublePrecision(HSFamily.feetToMM(connector.Width), 1);
							item.m_dHigh = Utils.DoublePrecision(HSFamily.feetToMM(connector.Height), 1);
						}
					}
					break;
				}
			}
			this.m_listSecSel.Add(item);
			return true;
		}

		public bool GetSecInfoFromSelPipes()
		{
			bool flag = this.m_listElementSel.Count<Element>() == 0;
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				this.m_listSecSel.Clear();
				bool flag2 = Utils.IsVerticalPipe(this.m_listElementSel[0]);
				if (flag2)
				{
					result = this.GetSecInfoFromSelVerPipe(this.m_listElementSel[0]);
				}
				else
				{
					for (int i = 1; i < this.m_listElementSel.Count<Element>(); i++)
					{
						Element element = this.m_listElementSel[i];
						bool flag3 = !Utils.Is2PipeParallel(element, this.m_listElementSel[0]) || Utils.IsVerticalPipe(element);
						if (flag3)
						{
							this.m_listElementSel.RemoveAt(i);
							i--;
						}
					}
					bool flag4 = this.m_listElementSel.Count == 0;
					if (flag4)
					{
						result = false;
					}
					else
					{
						Line line = null;
						double num = -1.0;
						foreach (Element element2 in this.m_listElementSel)
						{
							MEPCurve mepcurve = element2 as MEPCurve;
							bool flag5 = mepcurve == null;
							if (!flag5)
							{
								LocationCurve locationCurve = mepcurve.Location as LocationCurve;
								Line line2 = locationCurve.Curve as Line;
								bool flag6 = line2 == null;
								if (!flag6)
								{
									double length = line2.Length;
									bool flag7 = num > length || num < 0.0;
									if (flag7)
									{
										num = length;
										line = line2;
									}
								}
							}
						}
						bool flag8 = line == null;
						if (flag8)
						{
							result = false;
						}
						else
						{
							XYZ endPoint = line.GetEndPoint(0);
							XYZ endPoint2 = line.GetEndPoint(1);
							XYZ xyz = Utils.PtTo2d(endPoint);
							XYZ xyz2 = Utils.PtTo2d(endPoint2);
							XYZ xyz3 = (xyz2 - xyz).Normalize();
							XYZ xyz4 = xyz3.CrossProduct(XYZ.BasisZ);
							double num2 = 10000.0;
							XYZ xyz5 = xyz;
							bool bMirrored = this.m_bMirrored;
							if (bMirrored)
							{
								xyz4 *= -1.0;
							}
							result = this.GetSecInfoByLine(xyz5 - xyz4 * num2, xyz5 + xyz4 * num2, true);
						}
					}
				}
			}
			return result;
		}

		public static double GetHSElementWide(Element eleHS)
		{
			double result = 0.0;
			Parameter parameter = eleHS.GetParameter("净宽");
			bool flag = parameter == null;
			if (flag)
			{
				parameter = eleHS.GetParameter("宽");
			}
			else
			{
				bool flag2 = parameter == null;
				if (flag2)
				{
					parameter = eleHS.GetParameter("管径");
				}
				else
				{
					bool flag3 = parameter == null;
					if (flag3)
					{
						parameter = eleHS.GetParameter("竖杆高h");
					}
				}
			}
			bool flag4 = parameter != null;
			if (flag4)
			{
				result = HSFamily.feetToMM(parameter.AsDouble());
			}
			return result;
		}

		public UIApplication m_uiApp;

		public Document m_doc;

		public Selection m_selection;

		public List<Element> m_listElementSel;

		public List<PipeSectionDef> m_listSecSel;

		public XYZ m_ptPosition;

		public XYZ m_vtSection;

		public XYZ m_vtPipe;

		public double m_dAngle;

		public bool m_bMirrored;

		public XYZ m_ptPick;
	}
}
